Page({
    data: {
      currentFilter: 'popular', // 当前筛选条件：popular/sales/price
      priceSort: 'desc', // 价格排序：asc/desc
      services: [
        {
          id: 1,
          name: '脑中风术后康复套餐',
          image: '/static/images/service-rehab-1.jpg',
          diseaseTag: '脑血管疾病',
          therapyTag: '运动疗法',
          price: 1290,
          sales: 40,
          popularity: 95
        },
        {
          id: 2,
          name: '脑中风术后康复套餐',
          image: '/static/images/service-rehab-1.jpg',
          diseaseTag: '脑血管疾病',
          therapyTag: '运动疗法',
          price: 1290,
          sales: 40,
          popularity: 92
        },
        {
          id: 3,
          name: '脑中风术后康复套餐',
          image: '/static/images/service-rehab-1.jpg',
          diseaseTag: '脑血管疾病',
          therapyTag: '运动疗法',
          price: 1290,
          sales: 40,
          popularity: 88
        },
        {
          id: 4,
          name: '脑中风术后康复套餐',
          image: '/static/images/service-rehab-1.jpg',
          diseaseTag: '脑血管疾病',
          therapyTag: '运动疗法',
          price: 1290,
          sales: 40,
          popularity: 85
        }
      ]
    },
  
    onLoad: function(options) {
      // 页面加载时获取服务列表
      this.getServiceList()
    },
  
    // 返回上一页
    goBack: function() {
      wx.navigateBack()
    },
  
    // 切换筛选条件
    switchFilter: function(e) {
      const filter = e.currentTarget.dataset.filter
      if (filter === this.data.currentFilter && filter === 'price') {
        // 如果点击的是价格且已经是当前筛选条件，则切换排序方向
        this.setData({
          priceSort: this.data.priceSort === 'asc' ? 'desc' : 'asc'
        })
      } else {
        // 切换筛选条件
        this.setData({
          currentFilter: filter
        })
      }
      this.sortServices()
    },
  
    // 排序服务列表
    sortServices: function() {
      let services = [...this.data.services]
      switch (this.data.currentFilter) {
        case 'popular':
          services.sort((a, b) => b.popularity - a.popularity)
          break
        case 'sales':
          services.sort((a, b) => b.sales - a.sales)
          break
        case 'price':
          services.sort((a, b) => {
            if (this.data.priceSort === 'asc') {
              return a.price - b.price
            } else {
              return b.price - a.price
            }
          })
          break
      }
      this.setData({
        services: services
      })
    },
  
    // 获取服务列表
    getServiceList: function() {
      // 这里应该调用后端API获取服务列表
      // 目前使用模拟数据
      this.sortServices()
    },
  
    // 跳转到服务详情页
    goToDetail: function(e) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/services/rehabilitation/detail/index?id=${id}`
      })
    }
  })